package com.sdp.right.control;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_menu.service.ISysMenuService;
import com.sdp.right.db.sys_role_menu.service.ISysRoleMenuService;
import com.sdp.right.db.sys_system.service.ISysSystemService;
import com.sdp.right.entity.SysRoleMenu;
import com.sdp.right.entity.SysSystem;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-11
 */
@RestController
@RequestMapping("/SysRoleMenuController")
public class SysRoleMenuController {
	
	@Autowired
	private ISysRoleMenuService iSysRoleMenuService;
	
	@Autowired
	private ISysSystemService iSysSystemService;
	
	@Autowired
	private ISysMenuService iSysMenuService;

	
	/**
	 * 获取所有系统的菜单信息
	 * @param roleNo
	 * @return
	 */
	@GetMapping(value = "/getAllSysRoleMenu")
	public Result<?> getAllSysRoleMenu(@RequestParam(name="roleNo",required=true)String roleNo) {
		JSONObject obj = new JSONObject();
		List<SysSystem> sysTemList = iSysSystemService.list();
		List<SysRoleMenu> roleMenuList = iSysRoleMenuService.querySysRoleMenu(roleNo);
		if(sysTemList!=null && sysTemList.size()>0) {
			obj.put("systems", sysTemList);
			JSONObject systemMenu = new JSONObject();
			JSONObject systemMenuKeys = new JSONObject();
	
			for(SysSystem system : sysTemList) {
				//根据业务系统，查询所有的菜单信息
				JSONObject menuTreeObj = iSysMenuService.queryMenuTreeSelect(system.getSystemNo());
				systemMenu.put(system.getSystemNo(),menuTreeObj.get("treeData") );
				obj.put("treeData",systemMenu);
				
				//业务系统中，菜单内码
				systemMenuKeys.put(system.getSystemNo(),menuTreeObj.get("treeKeys") );
				obj.put("treeKeys",systemMenuKeys);
				
			
			}
			//此角色目前拥有的菜单权限
			List<String> menuNoList = null;
			Map<String,List<String>> systemMenuMap = new HashMap<>();
			for(SysRoleMenu rm : roleMenuList) {
				if(systemMenuMap.containsKey(rm.getSystemNo())) {
					systemMenuMap.get(rm.getSystemNo()).add(rm.getMenuNo());
				}else {
					menuNoList = new ArrayList<String>();
					menuNoList.add(rm.getMenuNo());
					systemMenuMap.put(rm.getSystemNo(), menuNoList);
				}
			}
			obj.put("roleMenuKeys",systemMenuMap);
			
			return Result.ok(obj);
		}else {
			return Result.error("业务系统信息不存在");
		}
	}
	
	/**
	 * 获取某个系统的菜单信息
	 * @param roleNo
	 * @param systemNo
	 * @return
	 */
	@GetMapping(value = "/getSysRoleMenu")
	public Result<?> getSysRoleMenuInfo(@RequestParam(name="roleNo",required=true)String roleNo,@RequestParam(name="systemNo",required=true)String systemNo) {
		JSONObject obj = new JSONObject();
		List<SysSystem> sysTemList = iSysSystemService.querySysSystemByNo(systemNo);
		List<SysRoleMenu> roleMenuList = iSysRoleMenuService.querySysRoleMenu(roleNo);
		if(sysTemList!=null && sysTemList.size()>0) {
			obj.put("systems", sysTemList);
			JSONObject systemMenu = new JSONObject();
			JSONObject systemMenuKeys = new JSONObject();
			for(SysSystem system : sysTemList) {
				//根据业务系统，查询所有的菜单信息
				JSONObject menuTreeObj = iSysMenuService.queryMenuTreeSelect(system.getSystemNo());
				systemMenu.put(system.getSystemNo(),menuTreeObj.get("treeData") );
				obj.put("treeData",systemMenu);
				
				//业务系统中，菜单内码
				systemMenuKeys.put(system.getSystemNo(),menuTreeObj.get("treeKeys") );
				obj.put("treeKeys",systemMenuKeys);
				
			
			}
			//此角色目前拥有的菜单权限
			List<String> menuNoList = null;
			Map<String,List<String>> systemMenuMap = new HashMap<>();
			for(SysRoleMenu rm : roleMenuList) {
				if(systemMenuMap.containsKey(rm.getSystemNo())) {
					systemMenuMap.get(rm.getSystemNo()).add(rm.getMenuNo());
				}else {
					menuNoList = new ArrayList<String>();
					menuNoList.add(rm.getMenuNo());
					systemMenuMap.put(rm.getSystemNo(), menuNoList);
				}
			}
			obj.put("roleMenuKeys",systemMenuMap);
			
			return Result.ok(obj);
		}else {
			return Result.error("业务系统信息不存在");
		}
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysRoleMenu sysRoleMenu) {
		if(sysRoleMenu==null) {
			Result.error("角色授权信息不存在");
		}
		//删除这个角色在这个系统下的所有菜单权限信息
		iSysRoleMenuService.removeSysRoleMenu(sysRoleMenu.getSystemNo(), sysRoleMenu.getRoleNo());

		//添加
		if(!StringUtils.isEmpty(sysRoleMenu.getMenuNo())) {
			List<SysRoleMenu> entityList = new ArrayList<SysRoleMenu>();
			String menuNoArr []= sysRoleMenu.getMenuNo().split(",");
			for(int i=0;i<menuNoArr.length;i++) {
				SysRoleMenu srm = new SysRoleMenu();
				srm.setRoleNo(sysRoleMenu.getRoleNo());
				srm.setSystemNo(sysRoleMenu.getSystemNo());
				srm.setMenuNo(menuNoArr[i]);
				entityList.add(srm);
			}
			iSysRoleMenuService.saveBatch(entityList);
		}
		
		return Result.ok();
	}
}

